package com.sam.spring.chapter5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sam.spring.chapter5.service.IHelloWorldService;
import com.sam.spring.chapter5.service.IIntroductionService;

public class AOPAnnotationTest {
	@Test
	public void testAnnotationBeforeAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice2.xml");
		IHelloWorldService helloWorldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloWorldService.sayAdvisorBefore("advice before...");
	}

	@Test
	public void testAnnotationAfterReturningAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice2.xml");
		IHelloWorldService helloWorldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloWorldService.sayAfterReturning();
	}

	@Test(expected = RuntimeException.class)
	public void testAnnotationAfterThrowing() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice2.xml");
		IHelloWorldService helloWorldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloWorldService.sayAfterThrowing();
	}

	@Test
	public void testAnnotationAfterFinally() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice2.xml");
		IHelloWorldService helloWorldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloWorldService.sayAfterFinally();
	}

	@Test
	public void testAnnotationAroundAdvice() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice2.xml");
		IHelloWorldService helloWorldService = ctx.getBean("helloWorldService",
				IHelloWorldService.class);
		helloWorldService.sayAround("haha..");
	}

	@Test
	public void testSchemaAnnotationIntroduction() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"chapter5/advice.xml");
		IIntroductionService introductionService = ctx.getBean(
				"helloWorldService", IIntroductionService.class);
		introductionService.induct();
	}
}
